package cn.myfirefly.moming.house.service;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.house.dao.SpfTradeContractDao;
import cn.myfirefly.moming.house.dao.SpfTradeContractWebimageDao;
import cn.myfirefly.moming.house.entity.SpfTradeContract;
import cn.myfirefly.moming.house.entity.SpfTradeContractWebimage;
import cn.myfirefly.moming.house.model.SpfTradeContractWebimageDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.CustomizedPropertyPlaceholderConfigurer;

@Service
@Transactional
public class SpfTradeContractWebimageService {

	@Autowired
	private SpfTradeContractWebimageDao spfTradeContractWebimageDao;
	
	@Autowired
	private SpfTradeContractDao spfTradeContractDao;
	
	public PageResultDto<SpfTradeContractWebimageDto> getList(SpfTradeContractWebimageDto dto) {
		
		return spfTradeContractWebimageDao.getList(dto);
	}
	
	public List<SpfTradeContractWebimage> getList(Integer contractId) {
		
		return spfTradeContractWebimageDao.getList(contractId);
	}
	
	public SpfTradeContractWebimageDto getSpfTradeContractWebimage(Integer id) {
		SpfTradeContractWebimage spfTradeContractWebimage = spfTradeContractWebimageDao.get(id);
		SpfTradeContractWebimageDto spfTradeContractWebimageDto = BeanUtil.copyBeans(SpfTradeContractWebimageDto.class, spfTradeContractWebimage);
		return spfTradeContractWebimageDto;
	}
	
	public String del(Integer id) {
		SpfTradeContractWebimage spfTradeContractWebimage = spfTradeContractWebimageDao.get(id);
		if (spfTradeContractWebimage == null) {
			return "该信息不存在，请尝试刷新列表后再操作。";
		}
		spfTradeContractWebimageDao.del(spfTradeContractWebimage);
		String picRealPath =  (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("picRealPath");
		// 删除文件
		File file = new File(picRealPath + spfTradeContractWebimage.getImgUrl());
		file.delete();
		return Const.ResultCode.SUCCESS;
	}
	
	public Integer insert(SpfTradeContractWebimageDto dto) {
		SpfTradeContractWebimage spfTradeContractWebimage = BeanUtil.copyBeans(SpfTradeContractWebimage.class, dto);
		int count = spfTradeContractWebimageDao.save(spfTradeContractWebimage);
		dto.setId(spfTradeContractWebimage.getId());
		return count;
	}
	
	public void submit(Integer contractId) {
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(contractId);
		spfTradeContract.setTradeContractStatus("3");
		spfTradeContractDao.save(spfTradeContract);
	}
	
	public Integer update(SpfTradeContractWebimageDto dto) {
		SpfTradeContractWebimage spfTradeContractWebimage = spfTradeContractWebimageDao.get(dto.getId());
		if (spfTradeContractWebimage == null) {
			return -1;
		}
		BeanUtil.copyBeans(dto, spfTradeContractWebimage);
		return spfTradeContractWebimageDao.save(spfTradeContractWebimage);
	}
	
	public Integer back(Integer contractId) {
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(contractId);
		if (spfTradeContract == null) {
			return -1;
		}
		spfTradeContract.setTradeContractStatus("3");
		return spfTradeContractDao.save(spfTradeContract);
	}
	
	public Integer pass(Integer contractId) {
		SpfTradeContract spfTradeContract = spfTradeContractDao.get(contractId);
		if (spfTradeContract == null) {
			return -1;
		}
		spfTradeContract.setTradeContractStatus("4");
		return spfTradeContractDao.save(spfTradeContract);
	}
}
